﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Helper;
using System.ServiceModel;
namespace OpenIDHostController
{
    public partial class frmMain : Form
    {
        ServiceHost host = new ServiceHost(typeof(OpenIDService));
        public frmMain()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            khởiĐộngLạiToolStripMenuItem.Enabled = false;
            dừngToolStripMenuItem.Enabled = false;

        }
     
        Thread ThreadAddMessage;
        private void cấuHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmConfig config = new frmConfig();
            config.ShowDialog();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void khởiĐộngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if(host==null)
                  host=  new ServiceHost(typeof(OpenIDService));
                host.Open();
                txtlog.Text += "The server is started" + Environment.NewLine; ;
                txtlog.Text +="Address: "+ host.BaseAddresses[0].AbsoluteUri + Environment.NewLine;
                lblstatus.Text = "Server is started";
                khởiĐộngToolStripMenuItem.Enabled = false;
                khởiĐộngLạiToolStripMenuItem.Enabled = true;
                dừngToolStripMenuItem.Enabled = true;

            }
            catch(Exception ex)
            {
                txtlog.Text += ex.Message + Environment.NewLine;
                lblstatus.Text = "Server is not started";
                MessageBox.Show("Server can not start at this time");
            }
         
           
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(!String.IsNullOrEmpty(ValueDefine.Message))
            {
                txtlog.Text += ValueDefine.Message + Environment.NewLine;
                //OpenIDHostController.Conmmand.ValueDefine.Message = "";
            }
        }

        private void khởiĐộngLạiToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                host.Close();
                host = null;
                host =new ServiceHost(typeof(OpenIDService));
                host.Open();
                lblstatus.Text = "Server is restarted";
                khởiĐộngToolStripMenuItem.Enabled = false;
                dừngToolStripMenuItem.Enabled = true;
                txtlog.Text += "Server is restarted" + Environment.NewLine;

            }catch(Exception ex)
            {
                txtlog.Text += ex.Message + Environment.NewLine;
                lblstatus.Text = "Server is not restarted";
                MessageBox.Show("Server can not restart at this time");
            }
           
        }

        void AddMess()
        {
           while(ThreadAddMessage.IsAlive)
           {
               try
               {
                   Application.DoEvents();
                   if (ValueDefine.LogMessage != null && ValueDefine.LogMessage.Count != 0)
                   {
                       foreach (var item in ValueDefine.LogMessage)
                       {
                           txtlog.Text += item + Environment.NewLine;
                           ValueDefine.LogMessage.Remove(item);
                       }
                      // ValueDefine.LogMessage.Clear();
                       //ValueDefine.Message = "";
                   }
                   Application.DoEvents();
               }
               catch { }
               Thread.Sleep(1);
           }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            ThreadAddMessage = new Thread(() => AddMess());
            ThreadAddMessage.Start();
        }

        private void txtlog_TextChanged(object sender, EventArgs e)
        {
            txtlog.SelectionStart = txtlog.Text.Length;
            txtlog.ScrollToCaret();
        }

        private void dừngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            host.Close();
            host = null;
            lblstatus.Text = "Server is stopped";
            txtlog.Text += "Server is stopped" + Environment.NewLine;
            khởiĐộngToolStripMenuItem.Enabled = true;
            khởiĐộngLạiToolStripMenuItem.Enabled = false;

        }

        private void restartApplicationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Application.ExecutablePath); // to start new instance of application
            Environment.Exit(0);//to turn off current app
        }
    }
}
